<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<ng-container  [formGroup]="colorRangeListFormGroup">
  <div class="flex flex-row items-center justify-start" *ngIf="advancedMode; else basicMode">
    <div class="tb-form-panel-title flex-1" translate>widgets.color.settings-type</div>
    <tb-toggle-select formControlName="advancedMode" class="flex-1">
      <tb-toggle-option [value]="false">
        {{ 'widgets.color.basic-mode' | translate }}
      </tb-toggle-option>
      <tb-toggle-option [value]="true">
        {{ 'widgets.color.advanced-mode' | translate}}
      </tb-toggle-option>
    </tb-toggle-select>
  </div>

  <ng-template #basicMode>
    <div class="flex flex-row items-center justify-start">
      <div *ngIf="panelTitle" class="tb-form-panel-title flex-1">{{ panelTitle }}</div>
    </div>
  </ng-template>

  <div class="tb-color-ranges" [ngSwitch]="colorRangeListFormGroup.get('advancedMode').value">
    <ng-template [ngSwitchCase]="false">
      <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
           (cdkDropListDropped)="rangeDrop($event, 'range')">
        <div cdkDrag class="tb-draggable tb-advanced-range-drag"
             [formGroup]="rangeFormGroup"
             *ngFor="let rangeFormGroup of rangeListFormGroups; trackBy: trackByRange; let $index = index; last as isLast;"
             [class.pb-2]="!isLast">
          <div class="flex flex-1 flex-row items-center justify-start">
            <div class="tb-form-row range-container">
              <div class="tb-value-range-text" translate>widgets.color.from</div>
              <mat-form-field appearance="outline" class="center number flex-1" subscriptSizing="dynamic">
                <input matInput type="number" formControlName="from" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>

              <div class="tb-value-range-text tb-value-range-text-to" translate>widgets.color.to</div>
              <mat-form-field appearance="outline" class="center number flex-1" subscriptSizing="dynamic">
                <input matInput type="number" formControlName="to" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>

              <tb-color-input asBoxInput
                              formControlName="color">
              </tb-color-input>
            </div>
            <button type="button"
                    mat-icon-button
                    class="tb-box-button"
                    (click)="removeRange($index)"
                    matTooltip="{{ 'action.remove' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </div>
          <button class="tb-box-button"
                  mat-icon-button
                  type="button"
                  cdkDragHandle
                  matTooltip="{{ 'action.drag' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>drag_indicator</mat-icon>
          </button>
        </div>
      </div>
    </ng-template>
    <ng-template [ngSwitchCase]="true">

      <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
           (cdkDropListDropped)="rangeDrop($event, 'rangeAdvanced')">
        <div cdkDrag class="tb-draggable tb-advanced-range-drag"
             *ngFor="let advancedRangeControl of advancedRangeControls; trackBy: trackByAdvancedRange;let $index = index; last as isLast;"
             [class.pb-2]="!isLast">
          <tb-advanced-range [formControl]="advancedRangeControl"
                             [aliasController]="aliasController"
                             [dataKeyCallbacks]="dataKeyCallbacks"
                             [datasource]="datasource"
                             (removeAdvancedRange)="removeAdvancedRange($index)">
          </tb-advanced-range>
          <button class="tb-box-button"
                  mat-icon-button
                  type="button"
                  cdkDragHandle
                  matTooltip="{{ 'action.drag' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>drag_indicator</mat-icon>
          </button>
        </div>
      </div>

    </ng-template>

  </div>
  <div class="tb-add-color-range">
    <button mat-stroked-button
            (click)="addRange()">
      <mat-icon>add</mat-icon>
    </button>
  </div>
</ng-container>
